#include "userview.h"
#include "ui_userview.h"

UserView::UserView(QWidget *parent,int uid,bool isVIP) :
    QMainWindow(parent),
    ui(new Ui::UserView)
{
    m_uid = uid;
    m_isVIP = isVIP;
    myDatabase.getConnection();

  /*  static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));


    ui->tableView->setModel(mode);*/

    ui->setupUi(this);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

UserView::~UserView()
{
    delete ui;
}

void UserView::on_Quit_clicked()
{
    this->~UserView();
    MainWindow* pmwd = new MainWindow();
    pmwd->show();
}


void UserView::on_c18_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("全部");
}




void UserView::on_c1_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '家居' ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("家居");
}


void UserView::on_c2_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '数码' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("数码");
}


void UserView::on_c3_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '服装' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("服装");
}


void UserView::on_c4_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '妆品' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("妆品");
}


void UserView::on_c5_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '清洁' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("清洁");
}


void UserView::on_c6_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '珠宝' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("珠宝");
}


void UserView::on_c7_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '运动' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("运动");
}


void UserView::on_c8_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '鞋袜' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("鞋袜");
}


void UserView::on_c9_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '母婴' ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("母婴");
}


void UserView::on_c10_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '玩具' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("玩具");
}


void UserView::on_c11_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '乐器' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("乐器");
}


void UserView::on_c12_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '食品' ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("食品");
}


void UserView::on_c13_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '艺术' ORDER BY gcat, name,price");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("艺术");
}


void UserView::on_c14_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '图书' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("图书");
}


void UserView::on_c15_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '教育' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("教育");
}


void UserView::on_c16_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '医药' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("医药");
}


void UserView::on_c17_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat = '文娱' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
    ui->label->setText("文娱");
}


void UserView::on_Select_clicked()
{
    QString str = ui->lineEdit->text();
    qDebug()<<str;
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT gcat,name,sname,price,num,info FROM tb_goodsinstore NATURAL JOIN tb_goods AS a NATURAL JOIN tb_store WHERE gcat LIKE '%"+str+"%' OR gname LIKE '%"+str+"%' ORDER BY gcat, name,price ");

    mode->setHeaderData(0,Qt::Horizontal,tr("cat"));
    mode->setHeaderData(1, Qt::Horizontal, tr("name"));
    mode->setHeaderData(2, Qt::Horizontal, tr("sname"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("info"));

    ui->tableView->setModel(mode);
}


void UserView::on_Add_clicked()
{
    int row= ui->tableView->currentIndex().row();
    QAbstractItemModel *model = ui->tableView->model ();
    QModelIndex index = model->index(row,0);//选中行第一列的内容
    QVariant cat = model->data(index);
    index = model->index(row,1);
    QVariant gname = model->data(index);
    index = model->index(row,2);
    QVariant sname = model->data(index);
    index = model->index(row,3);
    float price = model->data(index).toFloat();
    index = model->index(row,4);
    int actualnum = model->data(index).toInt();
    int num = ui->num->text().toInt();

    if(num>actualnum){
        QMessageBox::information(this,"提示","库存不足，请重新选择！！",QMessageBox::Yes);
        return;
    }
    QSqlQuery sql;
    sql.prepare("SELECT gid,sid FROM tb_goodsinstore NATURAL JOIN tb_store WHERE sname = :sname AND name = :name");
    sql.bindValue(":sname",sname);
    sql.bindValue(":name",gname);
    sql.exec();
    if(sql.first()){
        int gid = sql.value("gid").toInt();
        int sid = sql.value("sid").toInt();
        sql.prepare("INSERT INTO tb_trolly(tuser,tstore,tgoods,tcat,tprice,tnum,ttime) VALUES (:tuser,:tstore,:tgoods,:tcat,:tprice,:tnum,:ttime)");
        sql.bindValue(":tuser",m_uid);
        sql.bindValue(":tstore",sid);
        sql.bindValue(":tgoods",gid);
        sql.bindValue(":tcat",cat);
        sql.bindValue(":tprice",price);
        sql.bindValue(":tnum",num);
        QDateTime time;
        sql.bindValue(":ttime",time.currentDateTime());
        sql.exec();
        QMessageBox::information(this,"提示","添加成功！！",QMessageBox::Yes);

    }
}


void UserView::on_Mytrolly_clicked()
{
    trolly* pt = new trolly(this,m_uid,m_isVIP);
    pt->show();
}


void UserView::on_Info_clicked()
{
    UserInfo* pui = new UserInfo(this,m_uid);
    pui->show();
}


void UserView::on_Service_clicked()
{
    Client* pc = new Client(this,m_uid);
    pc->show();
}

